package com.distribute.portal.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.pojo.ColumnData;
import com.distribute.pojo.DbItem;
import com.distribute.portal.pojo.CollectionItem;
import com.distribute.portal.service.CollectionService;

import redis.clients.jedis.Tuple;

@Controller
public class CollectionController {
	@Autowired
	CollectionService collectionService;

	@RequestMapping(value = "/collection/check-collection/{itemId}", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
	@ResponseBody
	public String checkCollection(@PathVariable Long itemId, HttpServletRequest request, HttpServletResponse response) {
		boolean flag = collectionService.getCollectionItemById(itemId, request, response);
		if (flag) {
			return "exist";
		}
		return "";
	}

	@RequestMapping("/collection/collection")
	public String showCart(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<CollectionItem> list = collectionService.getCollectionItemList(request, response);
		System.out.println("++++++++++++++++++++++++++++++++" + list.toString());
		model.addAttribute("collectionList", list);
		return "collection";
	}

	// 添加商品到收藏夹
	@RequestMapping("/collection/add/{itemId}")
	public String addCollectionItem(@PathVariable Long itemId, HttpServletRequest request,
			HttpServletResponse response) {
		DistributeResult result = collectionService.addCollectionItem(itemId, request, response);
		return "redirect:/collection/success.html";
	}

	// 添加商品到收藏夹
	@RequestMapping(value = "/collection/rest/add/{itemId}", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
	@ResponseBody
	public String addCollectionItemById(@PathVariable Long itemId, HttpServletRequest request,
			HttpServletResponse response) {
		DistributeResult result = collectionService.addCollectionItem(itemId, request, response);
		return "success";
	}

	// 商品收藏成功页面
	@RequestMapping("/collection/success")
	public String showSuccess() {
		return "collectionSuccess";
	}

	@RequestMapping(value = "/collection/rest/delete/{itemId}", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
	@ResponseBody
	public String deleteCollectionItemById(@PathVariable Long itemId, HttpServletRequest request,
			HttpServletResponse response) {
		collectionService.deleteCollectionItem(itemId, request, response);
		return "success";
	}

	@RequestMapping("/collection/delete/{itemId}")
	public String deleteCartItem(@PathVariable Long itemId, HttpServletRequest request, HttpServletResponse response) {
		collectionService.deleteCollectionItem(itemId, request, response);
		return "redirect:/collection/collection.html";
	}

	@RequestMapping(value = "/revrange/{start}/{end}")
	@ResponseBody
	public DistributeResult getCollectionRevRange(@PathVariable long start, @PathVariable long end) {
		try {
			List<ColumnData> list = collectionService.getCollectionRevRange(start, end);
			return DistributeResult.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			return DistributeResult.build(500, "error");
		}
	}
}
